﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;

namespace Learn001.Controls
{
    public class PanelButton : Button
    {
        /// <summary>
        /// 当前按钮是否选中
        /// </summary>
        public static readonly DependencyProperty IsMaxProperty =
            DependencyProperty.Register("IsMax", typeof(bool), typeof(PanelButton), new PropertyMetadata(false, new PropertyChangedCallback(IsMaxChanged)));

        /// <summary>
        /// 当前按钮是否选中
        /// </summary>
        public bool IsMax
        {
            get { return (bool)GetValue(IsMaxProperty); }
            set { SetValue(IsMaxProperty, value); }
        }

        private static void IsMaxChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            PanelButton btn = sender as PanelButton;
            if (btn == null) return;

            if (btn.IsMax)
            {
                if (btn.IsMouseOver)
                    VisualStateManager.GoToState(btn, "MaxEnter", true);
                else
                    VisualStateManager.GoToState(btn, "Max", true);
            }
            else
            {
                if (btn.IsMouseOver)
                    VisualStateManager.GoToState(btn, "UnMaxHover", true);
                else
                    VisualStateManager.GoToState(btn, "UnMax", true);
            }
        }

        public PanelButton()
            : base()
        {
            this.DefaultStyleKey = typeof(PanelButton);
            this.Click += new RoutedEventHandler(MenuButton_Click);
            this.MouseEnter += new MouseEventHandler(PanelButton_MouseEnter);
            this.MouseLeave += new MouseEventHandler(PanelButton_MouseLeave);
        }

        void PanelButton_MouseLeave(object sender, MouseEventArgs e)
        {
            if (IsMax)
                VisualStateManager.GoToState(this, "Max", true);
            else
                VisualStateManager.GoToState(this, "UnMax", true);
        }

        void PanelButton_MouseEnter(object sender, MouseEventArgs e)
        {
            if (IsMax)
                VisualStateManager.GoToState(this, "MaxEnter", true);
            else
                VisualStateManager.GoToState(this, "UnMaxHover", true);
        }

        void MenuButton_Click(object sender, RoutedEventArgs e)
        {
            IsMax = !IsMax;
        }
    }
}
